﻿
$(function () {
    $(".from.datepicker").datepicker({
        changeMonth: true,
        numberOfMonths: 2,
        showOn: "button",
        onClose: function (selectedDate) {
            $(".to.datepicker").datepicker("option", "minDate", selectedDate);

            var compare = compareDiff(selectedDate, $(".to.datepicker").datepicker('getDate'));
            var hour = (compare === 0) ? $('.timepicker.end').timepicker('getHour') : 23;
            var minute = (compare === 0) ? $('.timepicker.end').timepicker('getMinute') : 59;
            $('.timepicker.start').timepicker('option', {
                maxTime: {
                    hour: hour,
                    minute: minute
                }
            });
            compare = compareDiff($('.timepicker.start').timepicker('getTimeAsDate'),
                           $('.timepicker.end').timepicker('getTimeAsDate'));
            if (compare < 0) {
                $('.timepicker.start').timepicker('setTime', $('.timepicker.end').timepicker('getTime'));
            }
        }
    });
    $(".to.datepicker").datepicker({
        changeMonth: true,
        numberOfMonths: 2,
        showOn: "button",
        onClose: function (selectedDate) {
            $(".from.datepicker").datepicker("option", "maxDate", selectedDate);

            var compare = compareDiff($(".from.datepicker").datepicker('getDate'), selectedDate);
            var hour = (compare === 0) ? $('.timepicker.start').timepicker('getHour') : 0;
            var minute = (compare === 0) ? $('.timepicker.start').timepicker('getMinute') : 0;
            $('.timepicker.end').timepicker('option', {
                minTime: {
                    hour: hour,
                    minute: minute
                }
            });
            compare = compareDiff($('.timepicker.start').timepicker('getTimeAsDate'),
                           $('.timepicker.end').timepicker('getTimeAsDate'));
            if (compare < 0) {
                $('.timepicker.end').timepicker('setTime', $('.timepicker.start').timepicker('getTime'));
            }
        }
    });

    $('.timepicker.start').timepicker({
        onSelect: tpStartSelect,
        showOn: 'button',
        button: '.timepicker_button_trigger.start'
    });
    $('.timepicker.end').timepicker({
        onSelect: tpEndSelect,
        showOn: 'button',
        button: '.timepicker_button_trigger.end'
    });

    $('.datepicker').attr('readonly', true);
    $('.timepicker').attr('readonly', true);
    $('.datepicker').change(function () {
        getDuration();
    });
    $('.timepicker').change(function () {
        getDuration();
    });
});

// when start time change, update minimum for end timepicker
function tpStartSelect(time, endTimePickerInst) {
    var compare = compareDiff($('.from.datepicker').datepicker('getDate'), $('.to.datepicker').datepicker('getDate'));
    var hour = (compare === 0) ? endTimePickerInst.hours : 0;
    var minute = (compare === 0) ? endTimePickerInst.minutes : 0;
    $('.timepicker.end').timepicker('option', {
        minTime: {
            hour: hour,
            minute: minute
        }
    });
}

// when end time change, update maximum for start timepicker
function tpEndSelect(time, startTimePickerInst) {
    var compare = compareDiff($('.from.datepicker').datepicker('getDate'), $('.to.datepicker').datepicker('getDate'));
    var hour = (compare === 0) ? startTimePickerInst.hours : 23;
    var minute = (compare === 0) ? startTimePickerInst.minutes : 59;
    $('.timepicker.start').timepicker('option', {
        maxTime: {
            hour: hour,
            minute: minute
        }
    });
}

function compareDiff(startDate, endDate) {
    var difference = (endDate - startDate) / (86400000);
    return difference;
}

function getDuration() {
    if ($('.from.datepicker').val() === '' || $('.to.datepicker').val() === '') return;
    var startDate = $('.from.datepicker').val() + ' ' + $('.timepicker.start').timepicker('getTime');
    var endDate = $('.to.datepicker').val() + ' ' + $('.timepicker.end').timepicker('getTime');
    startDate = getDateFromFormat(startDate, 'MM/dd/y HH:mm');
    endDate = getDateFromFormat(endDate, 'MM/dd/y HH:mm');

    var difference = (endDate - startDate);  // / (86400000);

    var duration = getDurationReadable(difference); // example time in millis

    $('#showDuration').text(duration);
}

function getDurationReadable(timeMillis) {
    var units = [
                { label: "millisenconds", mod: 1000 },
                { label: "seconds", mod: 60 },
                { label: "minutes", mod: 60 },
                { label: "hours", mod: 24 },
                { label: "days", mod: 7 },
                { label: "weeks", mod: 52 }
            ];
    var duration = new Object();
    var x = timeMillis;
    var result = '';
    for (i = 0; i < units.length; i++) {
        var tmp = x % units[i].mod;
        duration[units[i].label] = tmp;
        if (tmp > 0) {
            result = ', ' + tmp + ' ' + units[i].label + result;
        }
        x = (x - tmp) / units[i].mod
    }
    if (result.length > 2)
        return result.substring(2);
    return result;
}